package org.jeecg.modules.demo.cwSaleInvoice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.caiWuSkd.entity.CwSkd;
import org.jeecg.modules.demo.caiWuSkd.mapper.CwSkdMapper;
import org.jeecg.modules.demo.cwSaleInvoice.entity.CwSaleInvoice;
import org.jeecg.modules.demo.cwSaleInvoice.mapper.CwSaleInvoiceMapper;
import org.jeecg.modules.demo.cwSaleInvoice.service.ICwSaleInvoiceService;
import org.jeecg.modules.demo.soFeiyong.entity.SoFeiyong;
import org.jeecg.modules.demo.soFeiyong.mapper.SoFeiyongMapper;
import org.jeecg.modules.demo.soFyft.entity.SoFyft;
import org.jeecg.modules.demo.soFyft.mapper.SoFyftMapper;
import org.jeecg.modules.demo.soSaleOrder.mapper.SoSaleOrderMapper;
import org.jeecg.modules.demo.soSettlement.entity.SoSettlementVo;
import org.jeecg.modules.demo.util.generateOddUtilXs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: cw_sale_invoice
 * @Author: jeecg-boot
 * @Date: 2021-12-15
 * @Version: V1.0
 */
@Service
public class CwSaleInvoiceServiceImpl extends ServiceImpl<CwSaleInvoiceMapper, CwSaleInvoice> implements ICwSaleInvoiceService {

    @Resource
    private CwSaleInvoiceMapper cwSaleInvoiceMapper;
    @Resource
    private SoFeiyongMapper soFeiyongMapper;
    @Resource
    private SoFyftMapper soFyftMapper;
    @Resource
    private CwSkdMapper cwSkdMapper;
    @Resource
    private SoSaleOrderMapper soSaleOrderMapper;
    @Resource
    private ISysBaseAPI sysBaseAPI;

    private String getMessage(String name, String date) {
        String str = "由【" + name + "】于【" + date + "】发起的收款单需要您的审核，请及时查看！";
        return str;
    }

    private static final String TITLE = "单据审核";

    @Override
    public String getMaxCode() {
        return cwSaleInvoiceMapper.getMaxCode();
    }

    @Override
    @Transactional
    public boolean addSoSkd(JSONObject jsonObject, HttpServletRequest req) {
        try {
            CwSaleInvoice csi = JSON.parseObject(jsonObject.toJSONString(), CwSaleInvoice.class);
            CwSkd cs = JSON.parseObject(jsonObject.toJSONString(), CwSkd.class);
//            if (csi.getSyysJine().compareTo(csi.getSupamount()) == 1) {
//                return Result.error("本次使用预收金额不能大于剩余开票金额" + csi.getSupamount() + "!");
//            }


            String max_code = cwSkdMapper.getMaxCode();

            Map<String, String> codeMap = new HashMap<>();
            if (max_code == null) {
                codeMap.put("maxCode", null);
            } else {
                codeMap.put("maxCode", max_code);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId);
            String xssk = generateOddUtilXs.createCode(codeMap, tname + "SK_");

            CwSaleInvoice cwSaleInvoice = cwSaleInvoiceMapper.getByInvoiceNo(csi.getInvoiceNo());
            if (csi.getSrNo() != null && csi.getSrNo() != "" && !csi.getSrNo().isEmpty()) {
                CwSkd cskd = cwSkdMapper.getByskNo(csi.getSrNo());
                CwSkd yskdx = new CwSkd();//创建预收款抵扣单
                yskdx.setSkNo(xssk.substring(0,2)+"DK"+xssk.substring(4));
                yskdx.setSosNo(csi.getSosNo());
                yskdx.setFpNo(csi.getInvoiceNo());
                yskdx.setJsType(csi.getJsType());
                yskdx.setZhName(csi.getZhName());
                yskdx.setJsCustom(csi.getSoCustom());
                yskdx.setSosNum(csi.getFpsl());
                yskdx.setSosAmount(csi.getHsje());
                yskdx.setSosPrice(csi.getHsdj());
                yskdx.setSosNotax(csi.getWsje());
                yskdx.setYwName(csi.getPoStaff());
                yskdx.setDept(cs.getDept());
                yskdx.setSoNo(csi.getSoNo());
                yskdx.setYwType("4");
                yskdx.setSyys("0");
                yskdx.setIsDelete(0);
                yskdx.setSoStatus(0);
                yskdx.setTradecode(cs.getTradecode());
                if (csi.getSyysJine().compareTo(BigDecimal.valueOf(0)) > -1) { //>=0
                    if (cs.getLsJine() == null) {  //本次使用预收金额=cs.getSkJinen()
                        cskd.setSkJinen(cskd.getSkJinen().subtract(cs.getSkJinen()));//=0
                        cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(csi.getSyysJine()).subtract(cs.getSkJinen()));
                        if (cwSaleInvoice.getSyysJine() == null) {    //发票使用预收金额，
                            cwSaleInvoice.setSyysJine(cs.getSkJinen());
                        } else {
                            cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine().add(cs.getSkJinen()));
                        }
                        yskdx.setSkJinen(cs.getSkJinen());
                        cs.setSkJinen(csi.getSyysJine());//选择预收款单后，如果临时字段LsJine为null,则剩余预收款金额全部抵消
                        cs.setSyyskJine(yskdx.getSkJinen());
                        cs.setZskJine(cs.getSyyskJine().add(cs.getSkJinen()));

                    } else {   //本次使用预收金额=cs.getLsJine()
                        cskd.setSkJinen(cskd.getSkJinen().subtract(cs.getLsJine()));
                        cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(cs.getLsJine()).subtract(csi.getSyysJine()));
                        if (cwSaleInvoice.getSyysJine() == null) { //发票使用预收金额
                            cwSaleInvoice.setSyysJine(cs.getLsJine());
                        } else {
                            cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine().add(cs.getLsJine()));
                        }
                        yskdx.setSkJinen(cs.getLsJine());
                        cs.setSkJinen(csi.getSyysJine());
                        cs.setSyyskJine(cs.getLsJine());
                        cs.setZskJine(cs.getSyyskJine().add(cs.getSkJinen()));

                    }
                    if (cwSaleInvoice.getSupamount().compareTo(BigDecimal.valueOf(0)) <1) {   //<=0
                        cwSaleInvoice.setIsSkd(1);
                    }
                    cwSaleInvoice.setSrNo(csi.getSrNo());
                    cwSkdMapper.updateById(cskd);
                    cwSaleInvoiceMapper.updateById(cwSaleInvoice);
                    yskdx.setSkJinee(yskdx.getSkJinen());
                    yskdx.setBjJine(yskdx.getSkJinen());
                    yskdx.setPuCdate(new Date());
                    yskdx.setCreateTime(new Date());
                    yskdx.setSyyskNo(csi.getSrNo());
                    cwSkdMapper.insert(yskdx);

//                    return Result.OK("使用预收抵消发票金额成功，无须新增收款单!");

                    cs.setId("");
                    cs.setSkNo(xssk);
                    cs.setSosNo(csi.getSosNo());
                    cs.setFpNo(csi.getInvoiceNo());
                    cs.setSrNo(yskdx.getSkNo());//使用预收款单
                    cs.setSkJinee(cs.getSkJinen());
                    cs.setJsType(csi.getJsType());
                    cs.setZhName(csi.getZhName());
                    cs.setJsCustom(csi.getSoCustom());
                    cs.setSosNum(csi.getFpsl());
                    cs.setSosAmount(csi.getHsje());
                    cs.setSosPrice(csi.getHsdj());
                    cs.setSosNotax(csi.getWsje());
                    cs.setYwName(csi.getPoStaff());
                    cs.setSoNo(csi.getSoNo());
                    cs.setYwType("1");
                    cs.setSyys("1");
                    cs.setIsDelete(0);
                    cs.setSoStatus(0);
                    cs.setBjJine(cs.getSkJinen());
                    cs.setPuCdate(new Date());
                    cs.setCreateTime(new Date());
                    cs.setSyyskNo(csi.getSrNo());
                    cs.setBjSyyskno(cs.getSyyskNo());
                    cs.setBjYskjine(cs.getSyyskJine());
                    cwSkdMapper.insert(cs);


                    LoginUser user = sysBaseAPI.getUserById(cs.getYwName());
                    TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                    templateMessageDTO.setFromUser("管理员");
                    templateMessageDTO.setToUser(cs.getShName() + ",");
                    templateMessageDTO.setTitle(TITLE);
                    templateMessageDTO.setTemplateCode("order");
                    Map<String, String> templateParam = new HashMap<>();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = simpleDateFormat.format(new Date());
                    templateParam.put("ts_date", date);
                    templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
                    templateParam.put("checkOrder", cs.getSkNo());
                    templateParam.put("cd_url", "/collection/CwSkdList/list");
                    templateMessageDTO.setTemplateParam(templateParam);
                    sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

//                    return Result.OK("使用预收已抵消发票金额,并生成收款单成功!");
                }

            } else {
                if (csi.getSyysJine().compareTo(BigDecimal.valueOf(0)) == 1) {
                    cs.setId("");
                    cs.setSkNo(xssk);
                    cs.setSosNo(csi.getSosNo());
//			 String soSaleOrder = soSettlementService.getBySosNo(csi.getSosNo());
                    cs.setFpNo(csi.getInvoiceNo());
                    cs.setSkJinen(csi.getSyysJine());
                    cs.setJsType(csi.getJsType());
                    cs.setZhName(csi.getZhName());
                    cs.setJsCustom(csi.getSoCustom());
                    cs.setSosNum(csi.getFpsl());
                    cs.setSosAmount(csi.getHsje());
                    cs.setSosPrice(csi.getHsdj());
                    cs.setSosNotax(csi.getWsje());
                    cs.setYwName(csi.getPoStaff());
                    cs.setSoNo(csi.getSoNo());
                    cs.setSkJinee(cs.getSkJinen());
                    cs.setYwType("1");
                    cs.setSyys("0");
                    cs.setIsDelete(0);
                    cs.setSoStatus(0);
                    cs.setBjJine(cs.getSkJinen());
                    cs.setPuCdate(new Date());
                    cs.setCreateTime(new Date());
                    cs.setBjSyyskno(cs.getSyyskNo());
                    cs.setBjYskjine(cs.getSyyskJine());
                    cwSkdMapper.insert(cs);

                    LoginUser user = sysBaseAPI.getUserById(cs.getYwName());
                    TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                    templateMessageDTO.setFromUser("管理员");
                    templateMessageDTO.setToUser(cs.getShName() + ",");
                    templateMessageDTO.setTitle(TITLE);
                    templateMessageDTO.setTemplateCode("order");
                    Map<String, String> templateParam = new HashMap<>();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = simpleDateFormat.format(new Date());
                    templateParam.put("ts_date", date);
                    templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
                    templateParam.put("checkOrder", cs.getSkNo());
                    templateParam.put("cd_url", "/collection/CwSkdList/list");
                    templateMessageDTO.setTemplateParam(templateParam);
                    sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

                    cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(csi.getSyysJine()));

                    if (cwSaleInvoice.getSupamount().compareTo(BigDecimal.valueOf(0)) <1) {   //<=0
                        cwSaleInvoice.setIsSkd(1);
                    }
                    cwSaleInvoiceMapper.updateById(cwSaleInvoice);

//                    cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine());
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional
    public boolean editInvoice(JSONObject jsonObject, HttpServletRequest req) {
        try {
            CwSaleInvoice csi = JSON.parseObject(jsonObject.toJSONString(), CwSaleInvoice.class);
            CwSkd cs = JSON.parseObject(jsonObject.toJSONString(), CwSkd.class);
            if (csi.getSyysJine() == null) {
                csi.setSyysJine(BigDecimal.valueOf(0));
            }
//            if (csi.getSyysJine().compareTo(csi.getSupamount()) == 1) {
//                return Result.error("本次使用预收金额不能大于开票金额!");
//            }

            String max_code = cwSkdMapper.getMaxCode();

            Map<String, String> codeMap = new HashMap<>();
            if (max_code == null) {
                codeMap.put("maxCode", null);
            } else {
                codeMap.put("maxCode", max_code);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId);
            String xssk = generateOddUtilXs.createCode(codeMap, tname + "SK_");

            CwSaleInvoice cwSaleInvoice = cwSaleInvoiceMapper.getByInvoiceNo(csi.getInvoiceNo());
            if (csi.getSrNo() != null && csi.getSrNo() != "" && !csi.getSrNo().isEmpty()) {
                CwSkd cskd = cwSkdMapper.getByskNo(csi.getSrNo());
                CwSkd yskdx = new CwSkd();
                yskdx.setSkNo(xssk.substring(0,2)+"DK"+xssk.substring(4));
                yskdx.setSosNo(csi.getSosNo());
                yskdx.setFpNo(csi.getInvoiceNo());
                yskdx.setJsType(csi.getJsType());
                yskdx.setZhName(csi.getZhName());
                yskdx.setJsCustom(csi.getSoCustom());
                yskdx.setSosNum(csi.getFpsl());
                yskdx.setSosAmount(csi.getHsje());
                yskdx.setSosPrice(csi.getHsdj());
                yskdx.setSosNotax(csi.getWsje());
                yskdx.setYwName(csi.getPoStaff());
                yskdx.setDept(cs.getDept());
                yskdx.setSoNo(csi.getSoNo());
                yskdx.setYwType("4");
                yskdx.setSyys("0");
                yskdx.setIsDelete(0);
                yskdx.setSoStatus(0);
                yskdx.setTradecode(cs.getTradecode());
                if (csi.getSyysJine().compareTo(BigDecimal.valueOf(0)) > -1) { //>=0
                    if (cs.getLsJine() == null) {  //本次使用预收金额=cs.getSkJinen()
                        cskd.setSkJinen(cskd.getSkJinen().subtract(cs.getSkJinen()));//=0
                        cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(csi.getSyysJine()).subtract(cs.getSkJinen()));
                        if (cwSaleInvoice.getSyysJine() == null) {    //发票使用预收金额，
                            cwSaleInvoice.setSyysJine(cs.getSkJinen());
                        } else {
                            cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine().add(cs.getSkJinen()));
                        }
                        yskdx.setSkJinen(cs.getSkJinen());
                        cs.setSkJinen(csi.getSyysJine());//选择预收款单后，如果临时字段LsJine为null,则剩余预收款金额全部抵消
                        cs.setSyyskJine(yskdx.getSkJinen());
                        cs.setZskJine(cs.getSyyskJine().add(cs.getSkJinen()));
//                        cs.setFpsySkjine(cwSaleInvoice.getSupamount());

                    } else {   //本次使用预收金额=cs.getLsJine()
                        cskd.setSkJinen(cskd.getSkJinen().subtract(cs.getLsJine()));
                        cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(cs.getLsJine()).subtract(csi.getSyysJine()));
                        if (cwSaleInvoice.getSyysJine() == null) { //发票使用预收金额
                            cwSaleInvoice.setSyysJine(cs.getLsJine());
                        } else {
                            cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine().add(cs.getLsJine()));
                        }
                        yskdx.setSkJinen(cs.getLsJine());
                        cs.setSkJinen(csi.getSyysJine());
                        cs.setSyyskJine(cs.getLsJine());
                        cs.setZskJine(cs.getSyyskJine().add(cs.getSkJinen()));

                    }
                    if (cwSaleInvoice.getSupamount().compareTo(BigDecimal.valueOf(0)) <1) {   //<=0
                        cwSaleInvoice.setIsSkd(1);
                    }
                    cwSaleInvoice.setSrNo(csi.getSrNo());
                    cwSkdMapper.updateById(cskd);
                    cwSaleInvoiceMapper.updateById(cwSaleInvoice);
                    yskdx.setSkJinee(yskdx.getSkJinen());
                    yskdx.setBjJine(yskdx.getSkJinen());
                    yskdx.setPuCdate(new Date());
                    yskdx.setCreateTime(new Date());
                    yskdx.setSyyskNo(csi.getSrNo());
                    cwSkdMapper.insert(yskdx);

//                    return Result.OK("使用预收抵消发票金额成功，无须新增收款单!");

                    cs.setId("");
                    cs.setSkNo(xssk);
                    cs.setSosNo(csi.getSosNo());
                    cs.setFpNo(csi.getInvoiceNo());
                    cs.setSrNo(yskdx.getSkNo());//使用预收款单
                    cs.setSkJinee(cs.getSkJinen());
                    cs.setJsType(csi.getJsType());
                    cs.setZhName(csi.getZhName());
                    cs.setJsCustom(csi.getSoCustom());
                    cs.setSosNum(csi.getFpsl());
                    cs.setSosAmount(csi.getHsje());
                    cs.setSosPrice(csi.getHsdj());
                    cs.setSosNotax(csi.getWsje());
                    cs.setYwName(csi.getPoStaff());
                    cs.setSoNo(csi.getSoNo());
                    cs.setYwType("1");
                    cs.setSyys("1");
                    cs.setIsDelete(0);
                    cs.setSoStatus(0);
                    cs.setBjJine(cs.getSkJinen());
                    cs.setPuCdate(new Date());
                    cs.setCreateTime(new Date());
                    cs.setSyyskNo(csi.getSrNo());
                    cs.setBjSyyskno(cs.getSyyskNo());
                    cs.setBjYskjine(cs.getSyyskJine());
                    cwSkdMapper.insert(cs);


                    LoginUser user = sysBaseAPI.getUserById(cs.getYwName());
                    TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                    templateMessageDTO.setFromUser("管理员");
                    templateMessageDTO.setToUser(cs.getShName() + ",");
                    templateMessageDTO.setTitle(TITLE);
                    templateMessageDTO.setTemplateCode("order");
                    Map<String, String> templateParam = new HashMap<>();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = simpleDateFormat.format(new Date());
                    templateParam.put("ts_date", date);
                    templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
                    templateParam.put("checkOrder", cs.getSkNo());
                    templateParam.put("cd_url", "/collection/CwSkdList/list");
                    templateMessageDTO.setTemplateParam(templateParam);
                    sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

//                    return Result.OK("使用预收已抵消发票金额,并生成收款单成功!");
                }

            } else {
                if (csi.getSyysJine().compareTo(BigDecimal.valueOf(0)) == 1) {
                    cs.setId("");
                    cs.setSkNo(xssk);
                    cs.setSosNo(csi.getSosNo());
//			 String soSaleOrder = soSettlementService.getBySosNo(csi.getSosNo());
                    cs.setFpNo(csi.getInvoiceNo());
                    cs.setSkJinen(csi.getSyysJine());
                    cs.setJsType(csi.getJsType());
                    cs.setZhName(csi.getZhName());
                    cs.setJsCustom(csi.getSoCustom());
                    cs.setSosNum(csi.getFpsl());
                    cs.setSosAmount(csi.getHsje());
                    cs.setSosPrice(csi.getHsdj());
                    cs.setSosNotax(csi.getWsje());
                    cs.setYwName(csi.getPoStaff());
                    cs.setSoNo(csi.getSoNo());
                    cs.setSkJinee(cs.getSkJinen());
                    cs.setYwType("1");
                    cs.setSyys("0");
                    cs.setIsDelete(0);
                    cs.setSoStatus(0);
                    cs.setBjJine(cs.getSkJinen());
                    cs.setPuCdate(new Date());
                    cs.setCreateTime(new Date());
                    cs.setBjSyyskno(cs.getSyyskNo());
                    cs.setBjYskjine(cs.getSyyskJine());
                    cwSkdMapper.insert(cs);

                    LoginUser user = sysBaseAPI.getUserById(cs.getYwName());
                    TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                    templateMessageDTO.setFromUser("管理员");
                    templateMessageDTO.setToUser(cs.getShName() + ",");
                    templateMessageDTO.setTitle(TITLE);
                    templateMessageDTO.setTemplateCode("order");
                    Map<String, String> templateParam = new HashMap<>();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = simpleDateFormat.format(new Date());
                    templateParam.put("ts_date", date);
                    templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
                    templateParam.put("checkOrder", cs.getSkNo());
                    templateParam.put("cd_url", "/collection/CwSkdList/list");
                    templateMessageDTO.setTemplateParam(templateParam);
                    sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

                    cwSaleInvoice.setSupamount(cwSaleInvoice.getSupamount().subtract(csi.getSyysJine()));

                    if (cwSaleInvoice.getSupamount().compareTo(BigDecimal.valueOf(0)) <1) {   //<=0
                        cwSaleInvoice.setIsSkd(1);
                    }
                    cwSaleInvoiceMapper.updateById(cwSaleInvoice);

//                    cwSaleInvoice.setSyysJine(cwSaleInvoice.getSyysJine());
                }
            }
            cwSaleInvoiceMapper.updateById(csi);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    public CwSaleInvoice getByInvoiceNo(String invoiceNo) {
        return cwSaleInvoiceMapper.getByInvoiceNo(invoiceNo);
    }

    @Override
    public String getDepartId() {
        return cwSaleInvoiceMapper.getDepartId();
    }

    @Override
    public List<CwSaleInvoice> getCollectionJob(String parameter) {
        return cwSaleInvoiceMapper.getCollectionJob(parameter);
    }

    @Override
    public List<CwSaleInvoice> getlistBySosNo(String sosNo) {
        return cwSaleInvoiceMapper.getlistBySosNo(sosNo);
    }

    @Override
    public CwSaleInvoice getByskNo(String skNo) {
        return cwSaleInvoiceMapper.getByskNo(skNo);
    }

    @Override
    public List<CwSaleInvoice> getBysoNo(String id) {
        return cwSaleInvoiceMapper.getBysoNo(id);
    }

    @Override
    public List<CwSaleInvoice> getYkpBysoNo(String id) {
        return cwSaleInvoiceMapper.getYkpBysoNo(id);
    }

    @Override
    public List<SoSettlementVo> queryXseBymonth() {
        return cwSaleInvoiceMapper.queryXseBymonth();
    }

    @Override
    @Transactional
    public boolean editFyft(SoFyft soFyft) {
        try {
            CwSaleInvoice cws = cwSaleInvoiceMapper.getByInvoiceNo(soFyft.getFpNo());

            cws.setFtJine(soFyft.getFtJine().subtract(soFyft.getFtJine()));
            cwSaleInvoiceMapper.updateById(cws);
            SoFeiyong sf = soFeiyongMapper.getBySoFyNo(soFyft.getSoFyNo());
            sf.setFpNo("");
            sf.setSyFtjine(sf.getSyFtjine().add(soFyft.getFtJine()));
            if (sf.getSyFtjine().compareTo(BigDecimal.valueOf(0)) < 1) {  //剩余分摊金额<=0
                sf.setIsFt(2);
            } else {
                if (sf.getSyFtjine().compareTo(sf.getPsAmount()) == -1) {  //剩余分摊金额<分摊总金额
                    sf.setIsFt(1);
                } else {
                    sf.setIsFt(0);
                }
            }
            soFeiyongMapper.updateById(sf);
            soFyftMapper.deleteById(soFyft);

//            soFyftMapper.deleteByfpNo(cwSaleInvoice.getInvoiceNo());

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


}
